package main

import (
	"fmt"
	"github.com/jmoiron/sqlx"
)

func main() {
	dsn := "root:123456@tcp(127.0.0.12.9:3306)/gp_project"
	db, err := sqlx.Connect("mysql", dsn)
	if err != nil {
		panic(err)
	}
	defer db.Close()
	db.SetMaxOpenConns(10)

	tx, err := db.Beginx()
	if err != nil {
		panic(err)
	}

	row := tx.QueryRowx("select id from concurrent_exception_t where id = ? lock in share mode", 5)
	var id int
	row.Scan(&id)
	if id != 5 {
		fmt.Println("不存在插入数据")
		_, err = tx.Exec("INSERT INTO concurrent_exception_t (`id`, `name`, `sex`, `age`) VALUES (5, 'milo', 2, 20);")
		if err != nil {
			tx.Rollback()
			return
		}
	} else {
		fmt.Println("已经存在不插入")
	}
	tx.Commit()

}
